<!--
 * @Description:留言列表
 * @Author: zhangweigang
 * @Date: 2021-03-20 21:45:27
 * @LastEditTime: 2021-04-22 21:26:47
 * @LastEditors: zhangweigang
-->
<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }"><span class="tW">首页</span></el-breadcrumb-item>
      <el-breadcrumb-item><span class="tW">留言管理</span></el-breadcrumb-item>
      <el-breadcrumb-item><span class="tW">留言列表</span></el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索区域  :gutter 指定格与格的距离-->
      <el-row :gutter="20">
        <!--  :span 指定宽度 -->
        <el-col :span="8">
          <el-input placeholder="请输入留言主题" v-model="querySubject" clearable @clear="doClick">
            <el-button slot="append" @click="toClick" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 留言列表区域 -->
      <el-table class="table" :data="leaveMessageList" stripe border>
        <el-table-column prop="id" label="commentId" width="120" header-align="center"></el-table-column>
        <el-table-column prop="username" label="留言用户" width="150" header-align="center"></el-table-column>
        <el-table-column prop="subject" label="留言主题" width="250" header-align="center"></el-table-column>
        <el-table-column prop="content" label="留言内容" header-align="center"></el-table-column>
        <el-table-column prop="commentDate" label="留言日期" width="170" header-align="center"></el-table-column>
        <el-table-column label="操作" width="280" header-align="center">
          <template slot-scope="scope">
            <!-- 查看回复 -->
            <el-button type="warning" icon="el-icon-view" size="mini" @click="lookLeaveDialog(scope.row)">查看</el-button>
            <!-- 回复留言  -->
            <el-button type="primary" icon="el-icon-message" size="mini" @click="showLeaveDialog(scope.row)">回复</el-button>
            <!-- 删除留言  -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeLeaveByCommentId(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
        <!-- layout 指定功能组件 -->
      </el-pagination>
    </el-card>
    <!-- 回复消息 第一层 弹窗 -->
    <el-dialog title="消息回复" :visible.sync="leaveDialogVisible" @close="msgDialogClosed" width="50%">
      <!-- 内容主体区域 -->
      <el-form :model="leaveMessageForm" :rules="leaveMessageRules" ref="leaveMessageRef" label-width="100px">
        <el-form-item label="回复内容" prop="content">
          <el-input type="textarea" :rows="6" v-model="leaveMessageForm.content"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="leaveDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="leaveMessageInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看留言弹窗 -->
    <el-dialog title="留言内容" :visible.sync="dialogLookVisible">
      <div class="content_box" v-if="l > 0">
        <div class="btf_space" v-for="(item, index) in replayList" :key="index">
          <div class="left lb">
            <h3 class="lb">{{ item.toUsername }}</h3>
            <div class="touser lb">回复</div>
            <h3 class="lb">{{ item.username }}</h3>
            <p>#{{ item.replyDate }}</p>
          </div>
          <div class="right">
            <div class="info">
              <p class="lb">{{ item.content }}</p>
              <el-button class="btn lb" type="primary" size="mini" @click="replyInfoParams(item.commentId, item.id, item.toUserId, item.toUsername, item.userId, item.username)">回复</el-button>
            </div>
          </div>
          <!-- 第二层查看 -->
          <div class="second" v-for="(subItem, index) in item.nextNodes" :key="index">
            <div class="left">
              <h3 class="lb">{{ subItem.toUsername }}</h3>
              <span>回复</span>
              <h3 class="lb">{{ subItem.username }}</h3>
              <p class="lb">#{{ subItem.replyDate }}</p>
            </div>
            <div class="right">
              <div class="info">
                <p class="lb">{{ subItem.content }}</p>
                <el-button class="btn lb" type="warning" size="mini" @click="replyInfoParams(subItem.commentId, subItem.id, subItem.toUserId, subItem.toUsername, subItem.userId, subItem.username)"
                  >回复</el-button
                >
              </div>
            </div>
            <!-- 第三层查看 -->
            <div class="secondInfo" v-for="(thirdInfo, index) in subItem.nextNodes" :key="index">
              <div class="left">
                <h3 class="lb">{{ thirdInfo.username }}</h3>
                <span>回复</span>
                <h3 class="lb">{{ thirdInfo.toUsername }}</h3>
                <p class="lb">#{{ thirdInfo.replyDate }}</p>
              </div>
              <div class="right">
                <div class="info">{{ thirdInfo.content }}</div>
                <!-- 最多是能回复两层 <div class="btn">
                  <el-button type="warning" size="mini" @click="replyInfoParams(thirdInfo.commentId, thirdInfo.id, thirdInfo.toUserId, thirdInfo.toUsername, thirdInfo.userId, thirdInfo.username)">回复</el-button>
                </div> -->
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="no-info" v-else>还没有消息回复！请等待...</div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogLookVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogLookVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import bookShareManagerInfoAipService from '@/network/aipBookShareService.js'
export default {
  data() {
    return {
      leaveDialogVisible: false, // 留言消息弹窗
      dialogLookVisible: false, // 查看留言消息弹窗
      leaveMessageList: [],
      replayList: [],
      l: 0,
      querySubject: '',
      total: 0,
      pageNum: 1, // 当前页数
      pageSize: 10, // 每页显示多少条数据
      leaveMessageForm: {
        content: ''
      },
      leaveMessageRules: {
        content: [{ required: true, message: '回复内容不能为空！', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 保存管理员ID
    this.getCurrentAdminId()
    // 获取留言列表
    this.getLeaveList()
    // 留言分页查询
    this.getPageData(this.pageNum, this.pageSize)
  },
  methods: {
    // 保存管理员ID
    getCurrentAdminId() {
      bookShareManagerInfoAipService.getUserAllInfo().then(res => {
        if (res.data.code === 0) {
          for (let i = 0; i <= res.data.rows.length - 1; i++) {
            // 得到当前用户ID，并本地存储
            if (res.data.rows[i].username == localStorage.getItem('adminname')) {
              localStorage.setItem('adminId', res.data.rows[i].userId)
              return
            }
          }
        }
      })
    },
    toClick() {
      this.getSubjectQueryData(this.querySubject)
    },
    doClick() {
      this.getLeaveList()
      // 公告分页查询
      this.getPageData(this.pageNum, this.pageSize)
    },
    // 根据留言主体查询
    getSubjectQueryData(keys) {
      bookShareManagerInfoAipService.accordToSubjectQuery(keys).then(res => {
        if (res.data.code === 0) {
          this.total = res.data.total
          this.leaveMessageList = res.data.rows
        } else {
          this.$message({
            showClose: true,
            message: '查询失败，请联系管理员！',
            type: 'error',
            duration: 4000
          })
        }
      })
    },
    // 获取留言列表
    getLeaveList() {
      bookShareManagerInfoAipService.getLeaveInfoData().then(res => {
        if (res.data.code === 0) {
          this.total = res.data.total
        } else {
          this.$message({
            showClose: true,
            message: '留言获取失败，请联系管理员！',
            type: 'error',
            duration: 4000
          })
        }
      })
    },
    // 监听 pagesize 改变事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getPageData(this.pageNum, pageSize)
    },
    // 监听 页码值 改变事件
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.getPageData(pageNum, this.pageSize)
    },
    /**
     * 获取留言分页数据
     * @pageNum 当前页数
     * @pageSize 每页显示多少条数据
     */
    getPageData(pageNum, pageSize) {
      bookShareManagerInfoAipService.getLeavePagination(pageNum, pageSize).then(res => {
        if (res.data.code === 0) {
          // 获取留言列表
          this.leaveMessageList = res.data.rows
        } else {
          this.$message({
            showClose: true,
            message: '分页数据获取失败，请联系管理员！',
            type: 'error',
            duration: 4000
          })
          return
        }
      })
    },
    // 查看留言消息弹框
    lookLeaveDialog(scope) {
      bookShareManagerInfoAipService.getSoleReplayInfo(scope.id).then(res => {
        if (res.status === 200 && res.data.code === 0) {
          console.log(res.data.data)
          this.replayList = res.data.data
          this.l = this.replayList.length
          this.dialogLookVisible = true
        } else {
          this.$message({
            showClose: true,
            message: '未获取到留言，请联系管理员！',
            type: 'error',
            duration: 4000
          })
        }
      })
      this.dialogLookVisible = true
    },
    // 显示回复留言弹出框
    showLeaveDialog(scope) {
      console.log(scope, '信息')
      this.leaveDialogVisible = true
      // 第一层回复
      this.leaveMessageForm = {
        commentId: scope.id,
        replyId: scope.id, // 第一层回复 回复Id就是他自身
        toUserId: localStorage.getItem('adminId'),
        toUsername: localStorage.getItem('adminname'),
        userId: scope.userId,
        username: scope.username,
        delFlag: 1, // 未删除
        replyType: 1, // 给回复的回复
        sysRoleId: 0, // 必传参数，无需理会
        sysRoleName: '' // 必传参数，无需理会
      }
      console.log(this.leaveMessageForm, '132')
    },
    // 回复留言信息
    leaveMessageInfo() {
      console.log(this.leaveMessageForm, '参数')
      this.$refs.leaveMessageRef.validate(valid => {
        if (valid) {
          bookShareManagerInfoAipService.replyLeaveInfo(this.leaveMessageForm).then(res => {
            if (res.data.code === 0 && res.status === 200) {
              this.leaveDialogVisible = false
              this.$message({
                showClose: true,
                message: '恭喜您，回复成功！',
                type: 'success',
                duration: 4000
              })
            } else {
              this.$message({
                showClose: true,
                message: '回复失败，请联系管理员！',
                type: 'error',
                duration: 4000
              })
            }
          })
        } else {
          this.$message({
            showClose: true,
            message: '请先输入内容！',
            type: 'error',
            duration: 4000
          })
          return false
        }
      })
    },
    // 回复第二层
    replyInfoParams(commentId, replyId, toUserId, toUsername, userId, username) {
      this.dialogLookVisible = false
      this.leaveDialogVisible = true
      this.leaveMessageForm = {
        commentId: commentId,
        replyId: replyId,
        toUserId: userId,
        toUsername: username,
        userId: toUserId,
        username: toUsername,
        delFlag: 1, // 未删除
        replyType: 2, // 给回复的回复
        sysRoleId: 0, // 必传参数，无需理会
        sysRoleName: '' // 必传参数，无需理会
      }
      console.log(this.leaveMessageForm, '4561')
    },
    // 删除留言
    async removeLeaveByCommentId(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult, 'confirmResult')
      // 如果点击确定 返回为 confirm，点击取消 返回为 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除操作！')
      }
      bookShareManagerInfoAipService.removeByIDLeave(id).then(res => {
        if (res.data.code === 0) {
          this.$message.success('删除公告成功！')
          this.getLeaveList()
          // 刷新列表
          this.getPageData(this.pageNum, this.pageSize)
        } else {
          this.$message({
            showClose: true,
            message: '删除失败，请联系管理员！',
            type: 'error',
            duration: 4000
          })
          return
        }
      })
    },
    msgDialogClosed() {
      this.$refs.leaveMessageRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 15px;
}
.table {
  margin-top: 15px;
}
.second {
  margin-left: 30px;
}
.btf_space {
  padding: 10px;
  border-top: 1px solid #c1bcbc;
}
.lb {
  display: inline-block;
}
.left {
  width: auto;
  height: auto;
  color: #999;
  margin-right: 5px;
  h3 {
    color: #333;
  }
}
.left {
  p {
    font-size: 14px;
    margin: 0;
    font-family: Sentinel SSm A, Sentinel SSm B, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
  }
  h3 {
    margin: 0;
  }
}
.right {
  padding: 10px;
  background: #f3f3f3;
  border-radius: 5px;
  .info {
    position: relative;
    width: 100%;
    height: auto;
    color: #333;
    font-size: 15px;
    font-family: '微软雅黑';
    letter-spacing: 1px;
    p {
      margin: 0;
    }
  }
}
.btn {
  position: absolute;
  right: 5px;
  top: -3px;
}
.reply {
  button {
    padding: 5px 11px;
    margin: 0 3px;
    color: white;
    background: #016db5;
    font-size: 14px;
    border-radius: 5px;
    &:hover {
      background-color: #42a0df;
    }
  }
}
.no-info {
  text-align: center;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 7px;
  color: #999;
}
.content_box {
  height: 500px;
  overflow: auto;
}
</style>
